﻿using System;
using System.Linq;
using System.Web.Mvc;
using mvcForum.AddOns.ViewModels;
using mvcForum.Core.Interfaces.AddOns;
using mvcForum.Web.Controllers;
using mvcForum.Web.Helpers;
using mvcForum.Web.Interfaces;
using mvcForum.AddOns.NewUserTrial;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.AddOns.Controllers {

	public class NewUserTrialConfigurationController : ForumAdminBaseController, IAntiSpamConfigurationController {
		private readonly NewUserTrialConfiguration config;

		public NewUserTrialConfigurationController(IWebUserProvider userProvider, IContext context, IAddOnConfiguration<NewUserTrialAddOn> config)
			: base(userProvider, context) {

			this.config = (NewUserTrialConfiguration)config;
		}

		public ViewResult Index() {
			return View(new NewUserTrialViewModel { Enabled = this.config.Enabled, AutoLimit = this.config.AutoLimit, ExcludeGroups = this.config.ExcludeGroups.ToList(), RunAsynchronously = this.config.RunAsynchronously, Delay = this.config.Delay });
		}

		[HttpPost]
		public ViewResult Index(NewUserTrialViewModel model) {
			if (ModelState.IsValid) {
				this.config.Enabled = model.Enabled;
				this.config.AutoLimit = model.AutoLimit;
				this.config.RunAsynchronously = model.RunAsynchronously;
				this.config.Delay = model.Delay;

				this.config.ExcludeGroups = model.ExcludeGroups;

				this.Context.SaveChanges();

				TempData["Saved"] = true;
			}
			return View(model);
		}

		public string Name {
			get {
				return ForumHelper.GetString("Name", null, "mvcForum.AddOns.NewUserTrial");
			}
		}

		public string Description {
			get {
				return ForumHelper.GetString("Description", null, "mvcForum.AddOns.NewUserTrial");
			}
		}
	}
}